package com.ztgf.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.order.models.bo.LogisticsEditBO;
import com.ztgf.order.models.bo.OrderMyInvocieBO;
import com.ztgf.order.models.bo.OrderOpenUpInvoiceBO;
import com.ztgf.order.models.dto.OrderInvoiceDTO;
import com.ztgf.order.models.service.IOrderInvoiceService;
import com.ztgf.order.models.vo.OrderInvoiceDetailsVO;
import com.ztgf.order.models.vo.OrderInvoiceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单发票管理控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-03 15:58:16
 */

@RestController
@RequestMapping("/api/orderInvoice")
@Slf4j
@Api(value = "OrderInvoiceApiController", tags = "订单发票管理Api接口")
public class OrderInvoiceApiController {

    @DubboReference
    private IOrderInvoiceService orderInvoiceService;

    @PostMapping(value = "/queryMyOderInvocie")
    @ApiOperation(value = "个人中心，我的发票列表", notes = "个人中心，我的发票列表")
    public Result queryMyOderInvocie(OrderMyInvocieBO bo) {
        Page<OrderInvoiceVO> page = orderInvoiceService.queryMyOderInvocie(bo);
        return new Result<>(page);
    }

    @PostMapping(value = "/getOrderInvocie")
    @ApiOperation(value = "订单详情页发票信息", notes = "订单详情页发票信息")
    public Result getOrderInvocie(Long orderId, Long shopId) {
        OrderInvoiceDTO invoice = orderInvoiceService.getOrderInvoiceByOrderId(orderId, shopId);
        return new Result<>(invoice);
    }

    @PostMapping(value = "/updateOpenInvoice")
    @ApiOperation(value = "卖家个人中心发票列表去开票", notes = "卖家个人中心发票列表去开票")
    public Result updateOpenInvoice(OrderOpenUpInvoiceBO bo) {
        boolean flag = orderInvoiceService.updateOpenInvoice(bo);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100011);
        }
    }

    @PostMapping(value = "/updateLogistics")
    @ApiOperation(value = "修改物流信息（重新发货）", notes = "修改物流信息（重新发货）")
    public Result updateLogistics(LogisticsEditBO saveBO) {
        return orderInvoiceService.updateLogistics(saveBO);
    }

    @PostMapping(value = "/queryById")
    @ApiOperation(value = "根据id查询订单发票详情", notes = "根据id查询订单发票详情")
    public Result queryById(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            OrderInvoiceDetailsVO vo = orderInvoiceService.queryById(id);
            return new Result<>(vo);
        }
    }

    @PostMapping(value = "/updateById")
    @ApiOperation(value = "根据id更新发票数据", notes = "根据id更新发票数据")
    public Result updateById(OrderInvoiceDetailsVO orderInvoiceDetailsVO) {
        if (orderInvoiceDetailsVO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            boolean a=orderInvoiceService.updateOrderInvoiceById(orderInvoiceDetailsVO);
            if (a) {
                return new Result<>();
            } else {
                return new Result<>(CodeEnum.FAIL_100011);
            }
        }
    }


    @PostMapping(value = "/getByOrderId")
    @ApiOperation(value = "根据订单id查询发票信息", notes = "根据订单id查询发票信息")
    public Result<?> getByOrderId(Long orderId) {
        if (orderId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Wrapper<OrderInvoiceDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("order_id", orderId);
        OrderInvoiceDTO dto = orderInvoiceService.selectOne(wrapper);
        return new Result<>(dto);
    }

}
